//
//  E7TouchSprite.cpp
//  EndlessBattle
//
//  Created by cc on 12-5-2.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7TouchSprite.h"

#include "E7MenuLayer.h"

const int SPRITE_TOUCH_AREA = 50 ;

CE7TouchSprite::CE7TouchSprite(int nID, CCSprite* pNormal, CCSprite* pSelect,
                               CCSprite* pOtherC, CCSprite* pOtherL, CCSprite* pOtherR,
                               CCSprite* pNumberF, CCSprite* pNumberS, int nLv, CE7MenuLayer* pMenuLayer)
{
    init() ;
//    autorelease() ;

    m_nID = nID ;
    m_nLevel = nLv ;
    m_pNormal = pNormal ;
    m_pSelect = pSelect ;
    m_pLevel[0] = pOtherC ;
    m_pLevel[1] = pOtherL ;
    m_pLevel[2] = pOtherR ;
    m_pNumber[0] = pNumberF ;
    m_pNumber[1] = pNumberS ;

    m_pNormal->setIsVisible(true) ;
    m_pSelect->setIsVisible(false) ;

    addChild(m_pNormal) ;
    addChild(m_pSelect) ;
    for (int i = 2; i >= 0; --i)
    {
        addChild(m_pLevel[i]) ;
    }

    Hide() ;
    SetLevel() ;

    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false) ;

    m_pParent = pMenuLayer ;
}

CE7TouchSprite::~CE7TouchSprite()
{
//    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false) ;
}

void CE7TouchSprite::SetLevel()
{
    for (int i = 0; i < 3; ++i)
    {
        m_pLevel[i]->setIsVisible(false) ;
    }

    for (int i = 0; i < m_nLevel; ++i)
    {
        m_pLevel[i]->setIsVisible(true) ;
    }
}

void CE7TouchSprite::Show()
{
    m_pNumber[0]->setIsVisible(true) ;
    m_pNumber[1]->setIsVisible(true) ;

    setIsVisible(true) ;
}

void CE7TouchSprite::Hide()
{
    m_pNumber[0]->setIsVisible(false) ;
    m_pNumber[1]->setIsVisible(false) ;
    
    setIsVisible(false) ;
}

void CE7TouchSprite::onEnter()
{
//    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false) ;
//    printf("enter CE7TouchSprite::onEnter [%d]\n", m_nID) ;
}

void CE7TouchSprite::onExit()
{
//    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false) ;
//    printf("enter CE7TouchSprite::onExit [%d]\n", m_nID) ;
}

bool CE7TouchSprite::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    if (!getIsVisible())
    {
        return false ;
    }

    CCPoint pos = this->convertTouchToNodeSpace(pTouch) ;
    pos = convertToWorldSpace(pos) ;
    CCPoint res = ccpSub(getPosition(), pos) ;
    if (SPRITE_TOUCH_AREA >= sqrt(res.x*res.x+res.y*res.y))
    {
//        printf("-------------- enter CE7TouchSprite::ccTouchBegan [%d][%f][%f]\n", m_nID, getPosition().x, getPosition().y) ;
        m_pSelect->setIsVisible(true) ;
    }
    else
    {
        m_pSelect->setIsVisible(false) ;
    }

    return true ;
}

void CE7TouchSprite::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
    if (!getIsVisible())
    {
        return ;
    }

    CCPoint pos = this->convertTouchToNodeSpace(pTouch) ;
    pos = convertToWorldSpace(pos) ;
    CCPoint res = ccpSub(getPosition(), pos) ;
    if (SPRITE_TOUCH_AREA >= sqrt(res.x*res.x+res.y*res.y))
    {
//        printf("-------------- enter CE7TouchSprite::ccTouchMoved [%d][%f][%f]\n", m_nID, getPosition().x, getPosition().y) ;
        m_pSelect->setIsVisible(true) ;
    }
    else
    {
        m_pSelect->setIsVisible(false) ;
    }
}

void CE7TouchSprite::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
    if (!getIsVisible())
    {
        return ;
    }
    CCPoint pos = this->convertTouchToNodeSpace(pTouch) ;
    pos = convertToWorldSpace(pos) ;
//    printf("enter CE7TouchSprite::ccTouchEnded [%f][%f]\n", pos.x, pos.y) ;
//    printf("enter CE7TouchSprite::ccTouchEnded [%d][%f][%f]\n", m_nID, getPosition().x, getPosition().y) ;
    CCPoint res = ccpSub(getPosition(), pos) ;
    if (SPRITE_TOUCH_AREA >= sqrt(res.x*res.x+res.y*res.y))
    {
//        printf("-------------- enter CE7TouchSprite::ccTouchEnded [%d][%f][%f]\n", m_nID, getPosition().x, getPosition().y) ;
        if (s_bPlaySound)
        {
            SimpleAudioEngine::sharedEngine()->playEffect(sound_button) ;
        }
        m_pSelect->setIsVisible(true) ;
        m_pParent->StartNormalGame(m_nID) ;
    }
    else
    {
        m_pSelect->setIsVisible(false) ;
    }
}

void CE7TouchSprite::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
    printf("enter CE7TouchSprite::ccTouchCancelled\n") ;
}


















